/*
 * main.c
 * Copyright (C) Simone Vellei 2009 <henomis@gmail.com>
 *
	doitrd is free software: you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the
	Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	doitrd is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*Program closes with a mouse click or keypress */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <string.h>
#include "sha1.h"
#include "rand.h"
#include "protocol.h"
#include "sa.h"

#define DEFAULT_MAIN 0


int main (int argc, char *argv[])
{

#if DEFAULT_MAIN	
	Display              *dpy;
	Visual               *visual;
	int                   depth;
	XSetWindowAttributes  attributes;
	Window                win;
	XFontStruct         *fontinfo;
	XColor               color, dummy;
	XGCValues            gr_values;
	GC                   gc;
	XKeyEvent event;

	dpy = XOpenDisplay(NULL);
	visual = DefaultVisual(dpy, 0);
	depth  = DefaultDepth(dpy, 0);
	attributes.background_pixel = XWhitePixel(dpy, 0);
	/* create the application window */
	win = XCreateWindow(dpy, XRootWindow(dpy, 0),
			50, 50, 400, 400, 5, depth,
			InputOutput, visual, CWBackPixel,
			&attributes);
	XSelectInput(dpy, win, ExposureMask | KeyPressMask |
			ButtonPressMask | StructureNotifyMask);

	fontinfo = XLoadQueryFont(dpy, "6x10");
	XAllocNamedColor(dpy, DefaultColormap(dpy, 0),
		"green", &color, &dummy);
	gr_values.font = fontinfo->fid;
	gr_values.foreground = color.pixel;
	gc = XCreateGC(dpy, win, GCFont+GCForeground, &gr_values);
	XMapWindow(dpy, win);

	/* run till key press */
	while(1){
		XNextEvent(dpy, (XEvent *)&event);
		switch(event.type) {
			case Expose:
				XDrawLine(dpy, win, gc, 0, 0, 100, 100);
				XDrawRectangle(dpy, win, gc, 140, 140, 50, 50);
				XDrawString(dpy, win, gc, 100, 100, "hello X world", 13);
				break;
			case ButtonPress:
			case KeyPress:
				XUnloadFont(dpy, fontinfo->fid);
				XFreeGC(dpy, gc);
				XCloseDisplay(dpy);
				exit(0);
				break;
			case ConfigureNotify:
				/* reconfigure size of window here */
				break;
			default:
				break;
		}
	}
	return(0);

#endif

	int i,j,len;
	SHA1_CTX context;
	unsigned char digest[20], buffer[16384];
	unsigned int last_seqnum = 0;

	SHA1Init(&context);


	sprintf((char*)buffer,"%s","ciao");
	len = 4;
	SHA1Update(&context, buffer, len);
	SHA1Final(digest, &context);

	printf("Testing sha1 functions...\n");
	for (i = 0; i < 5; i++) {
		for (j = 0; j < 4; j++) {
			printf("%02X", digest[i*4+j]);
		}
	}
	
	putchar('\n');
	putchar('\n');

	printf("testing PRNG function\n");
        printf("rand() = %d\n", HX_rand());
        printf("irand(0,100) = %d\n", HX_irand(0,100));
        printf("irand(0,10000) = %d\n\n", HX_irand(0,10000));

	packet_t pkg, pkg2;
	char *chiave = "ciaomamma";

	printf("Testing packet\n");
	init_packet(&pkg, (unsigned char*)chiave);
	init_packet(&pkg2, (unsigned char*)chiave);
	
	prepare_packet(&pkg, 0x55, 0x66,1,0);
	prepare_packet(&pkg2, 0x44, 0x33,0,555);

	printf("--------\n\n\n");
	info_packet(&pkg);
	info_packet(&pkg2);
	printf("--------\n\n\n");

	if(check_packet(&pkg2,(unsigned char*)chiave, last_seqnum) == 0)
		printf("valid packet\n");
	else 
		printf("NOT valid packet\n");

	last_seqnum = 555;

	if(check_packet(&pkg2,(unsigned char*)chiave, last_seqnum) == 0)
		printf("valid packet\n");
	else 
		printf("NOT valid packet\n");


	//test sa.h

	sa_t *sa_srv;
	sa_t *sa_cld;
	sa_addr_t *saa_cld;
	sa_addr_t *saa_srv;
	
	sa_create(&sa_srv);
        sa_option(sa_srv, SA_OPTION_REUSEADDR, 1);
        sa_option(sa_srv, SA_OPTION_REUSEPORT, 1);
        sa_timeout(sa_srv, SA_TIMEOUT_ALL, 10, 0);
        sa_timeout(sa_srv, SA_TIMEOUT_ACCEPT, 0, 0);

	/* bind socket to local port */
        sa_addr_create(&saa_srv);
        sa_addr_u2a(saa_srv, "inet://127.0.0.1:12345#tcp");
        sa_bind(sa_srv, saa_srv);
        sa_addr_destroy(saa_srv);

        /* receive client connection */
        sa_listen(sa_srv, 10);
        sa_accept(sa_srv, &saa_cld, &sa_cld);
	

/*	while(1==1) {
	}*/
	
	/* destroy client connection */
        sa_destroy(sa_cld);
        sa_addr_destroy(saa_cld);

        /* destroy server socket and die */
        sa_destroy(sa_srv);


	return 0;
	
}
